package com.fingard.dsp.bank.directbank.shie01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.io.FileHelper;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Date;

public class SHIE01AcceptNotice extends SHIE01Base implements IAcceptNotice {
	
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
    	NoticeResp resp = new NoticeResp();
    	String p_req = pNtReq.getReqString();
    	WriteBankLogLn("接收到请求：" + pNtReq.getFullString());
        if (StringHelper.hasAnyChar(p_req) && p_req.indexOf("{") >= 0) {
            String eachJson = p_req.substring(p_req.indexOf("{"));
            Date notifyTime = new Date();
            String tmpDirByDate = Format.Date7YearMonth.format(notifyTime).replace("-", File.separator);
            String tmpSubNotifyFile = "Notice" + File.separator + tmpDirByDate + File.separator + reqHeader.bankCode + File.separator + reqHeader.bankCode + "_" + Format.Date10Format.format(notifyTime) + ".txt";
            String tmpNotifyFile = FGBiz.dspCenter.bankLogsDirPath + File.separator + tmpSubNotifyFile;
            try {
                JSONObject jsonObj = JSON.parseObject(eachJson);
                if (jsonObj.getString("payResult").equals("07")) {
                    FileHelper.appendFile(tmpNotifyFile, eachJson + "\r\n", FGBiz.dspCenter.charset);
                }
                String tmpStrRet = "SUCCESS";
                WriteBankLogLn("响应："+tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
            	return resp;
            } catch (Exception e) {
                this.WriteBankLogLn(e);
            }
        }
        String tmpStrRet = "FAIL";
        WriteBankLogLn("响应："+tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
    	return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {

    }
}
